package com.yidu.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.dao.SnBomMapper;
import com.yidu.dao.SnCommodityMapper;
import com.yidu.dao.SnMaInventoryMapper;
import com.yidu.dao.SnMaterialsMapper;
import com.yidu.dao.SnProDetailMapper;
import com.yidu.dao.snManufactureMapper;
import com.yidu.model.SnBom;
import com.yidu.model.SnCommodity;
import com.yidu.model.SnMaInventory;
import com.yidu.model.SnMaterials;
import com.yidu.model.SnProDetail;
import com.yidu.model.snManufacture;
import com.yidu.service.SnProDetailService;
/**
 * 生产计划详情实现类
 * @author chensituo
 * @date 2018年11月27日 上午12:47:15
 */
@Service
public class SnProDetailServiceImpl  implements SnProDetailService{
	/**
	 * 注入生产详情mapper
	 */
	@Resource
	SnProDetailMapper proDetailMapper;
	/**
	 * 注入物料清单mapper
	 */
	@Resource
	SnBomMapper bomMapper;
	/**
	 * 注入材料库存mapper
	 */
	@Resource
	SnMaInventoryMapper inventoryMapper;
	/**
	 * 注入商品表mapper
	 */
	@Resource
	SnCommodityMapper commodityMapper;
	/**
	 * 注入材料表mapper
	 */
	@Resource
	SnMaterialsMapper materialsMapper;
	/**
	 * 注入制造计划mapper
	 */
	@Resource
	snManufactureMapper manufactureMapper;
	
	
	@Override
	public List<SnProDetail> findDuction(Map<String, Object> maps) {
		//调用根据生产id查询生产详情的方法
		return proDetailMapper.findDuction(maps);
	}

	@Override
	public int updateByPrimaryKeySelective(SnProDetail detail) {
		//调用根据id修改的方法
		return proDetailMapper.updateByPrimaryKeySelective(detail);
	}

	@Override
	public int delete(String deId) {
		//调用根据id查询的方法
		SnProDetail detail=proDetailMapper.selectByPrimaryKey(deId);
		detail.setIsva(0);
		//创建制造计划model
		snManufacture manufacture=manufactureMapper.selectDeId(deId);
		manufacture.setIsva(0);
		//调用制造计划修改的方法
		manufactureMapper.updateByPrimaryKeySelective(manufacture);
		//调用根据id修改的方法
		return proDetailMapper.updateByPrimaryKeySelective(detail);
	}

	@Override
	public int findCount(Map<String, Object> maps) {
		//调用根据条件查询行数的方法
		return proDetailMapper.findCount(maps);
	}

	@Override
	public int insert(SnProDetail detail) {
		return proDetailMapper.insertSelective(detail);
	}

	@Override
	public SnProDetail selectProCom(SnProDetail detail) {
		return proDetailMapper.selectProCom(detail);
	}

	@Override
	public List<SnProDetail> findProId(String proId) {
		//调用根据生产id查询的方法
		return proDetailMapper.findProId(proId);
	}

	@Override
	public String selectStock(String number, String comId) {
		//查询该商品的物料清单
		List<SnBom> listBom=bomMapper.selectComId(comId);
		for (SnBom snBom : listBom) {
			//根据材料id查询材料库存
			SnMaInventory inventory=inventoryMapper.selectMaId(snBom.getMaId());
			//根据商品id查询
			SnCommodity commodity=commodityMapper.selectByPrimaryKey(comId);
			//根据材料id查询
			SnMaterials materials=materialsMapper.selectByPrimaryKey(snBom.getMaId());
			if(inventory==null || "".equals(inventory)) {
				return "抱歉,"+commodity.getComName()+"的原材料"+materials.getMaName()+"库存不足";
			}else {
				//得到生产该商品需要的材料总数量
				Integer proNumber = Integer.valueOf(snBom.getBomNumber())*Integer.valueOf(number);
				//如果需要的数量大于材料库存数量
				if(proNumber>inventory.getInNumber()) {
					//库存不足
					return "抱歉,"+commodity.getComName()+"的原材料"+materials.getMaName()+"库存不足"+(proNumber-inventory.getInNumber())+"个";
				}
			}
			
		}
		return "1";
	}
}
